<template>
	<view>
		<u-field @click="companyFn" :field-style="fieldStyle" v-model="company" label="开户公司" placeholder="请选择开户公司"
			disabled right-icon="arrow-right" @right-icon-click="companyFn">
			<!-- <u-icon name="arrow-right"></u-icon> -->
		</u-field>
		<u-field v-model="mobile" label="资金账户" :field-style="fieldStyle" placeholder="请输入资金账号"></u-field>
		<u-field v-model="code" label="交易密码" :field-style="fieldStyle" :password="password" placeholder="请输入交易密码"
			right-icon="eye" @right-icon-click="rightclick"></u-field>
		<!-- <view class="nopass" @click="nopass">修改密码</view> -->
		<u-button class="custom-style" @click="dealLoginFn">马上登陆</u-button>
		<view class="deal" @click="noDeal">
			无法登陆 <u-icon name="arrow-right-double"></u-icon>
		</view>
		<view class="bottom_text">
			非交易时间不可登陆（工作日16:30-20:00和周末全天）
		</view>
		<view class="">
			<!-- <u-button class="custom-style" @click="initAlert">登陆</u-button> -->
			<!-- {{wdata}} -->
		</view>
		<view class="">
			<!-- <u-button class="custom-style" @click="initfa">测登陆</u-button> -->
			<!-- {{Data.brokerId}},{{Data.authCode}},{{Data.userId}},{{Data.appId}},{{Data.userProductInfo}},{{Data.userProductInfo}},{{Data.serviceUrl}},{{Data.password}} -->
		</view>
		<view class="">
			<!-- {{tdata}} -->
			<!-- <u-button class="custom-style" @click="Account1">测获取</u-button> -->
		</view>
		<view class="">
			<!-- <u-button class="custom-style" @click="Account">获取</u-button> -->
			<!-- {{tdata.data}} -->
		</view>
		<u-popup width="70%" v-model="ismobile" mode="center" border-radius="14">
			<view class="mobile_cont">
				<view class="title">温馨提示</view>
				<view class="text">
					期货实盘账户可登录时间为交易日的
				</view>
				<view class="time"> <text class="">8:00~16:30</text> <text class="">20:30~次日2:30</text></view>
				<view class="buttom">
					<view class="left" @click="ismobile=false">我知道了</view>
					<view class="right" @click="noDeal">查看详情</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getBroker,
		isOnline,
		affirmJieSuan
	} from '@/api/strategy.js'
	import {
		init,
		Traderinit,
		reqQrySecAgentTradingAccount
	} from '@/api/trader.js'
	import {
		parseTime
	} from '@/utils/index.js'
	import {
		frequencyInfo
	} from '@/api/member.js'
	var dcRichAlert = uni.requireNativePlugin('Trader')

	export default {
		data() {
			return {
				company: '',
				mobile: '',
				code: '',
				wdata: [],
				Data: [],
				tdata: [],
				ismobile: false,
				password: true,
				dealData_1: '',
				fieldStyle: {
					fontSize: '13px',
					padding: '10px'
				}
			}
		},
		onShow() {
			uni.showTabBar()
			//this.company = uni.getStorageSync('comLogin');
			if (getApp().globalData.dealData != '') {
				this.company = getApp().globalData.dealData.name
				let comLogin = uni.getStorageSync('comLogin');
				const usertion = uni.getStorageSync('userPosition')
			} else {
				getBroker().then(res => {
					getApp().globalData.dealData = uni.getStorageSync('comLogin');
				})
			}
			try {
				const usertion = uni.getStorageSync('userPosition')
				if (usertion) {
					this.mobile = usertion.user
					this.code = usertion.password
				}
			} catch (e) {
				//error
			}
		},
		mounted() {
			let comLogin = uni.getStorageSync('comLogin');
			this.company = comLogin.name
		},
		methods: {
			frequency() {
				let user_id = uni.getStorageSync('user_id')
				uni.request({
					url: 'https://wechat.sou-liang.com/api/v1/frequency',
					method: 'POST',
					data: {
						type: 2,
						id: user_id,
						account : this.mobile
					},
					success: (res) => {
						let data = res.data.data
					}
				});
			},
			companyFn() {
				uni.redirectTo({
					url: './company/company'
				})
			},
			rightclick() {
				this.password = !this.password
			},
			nopass() {
				uni.navigateTo({
					url: '../forgetPassword/index'
				})
				this.ismobile = false
			},
			dealLoginFn() {
				let that = this
				this.frequency()
				if (this.mobile == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入账号'
					})
					return false
				}
				if (this.code == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入密码'
					})
					return false
				}
				uni.showLoading({
					title: '登录中',
					mask: true
				});
				let {
					broker_id,
					auth_code,
					appid,
					trader_url,
					trader_port,
					product_info,
					trader2_url,
					trader2_port
				} = getApp().globalData.dealData
				uni.setStorageSync('dealData', getApp().globalData.dealData);
				this.dealData_1 = uni.getStorageSync('dealData')
				// let tell={
				// 	ip:trader_url,
				// 	port:trader_port,
				// }

				let tell = {
					ip: this.dealData_1.trader_url,
					port: this.dealData_1.trader_port,
				}
				// affirmJieSuan({account:this.mobile}).then(res=>{
				// 	if(res.jiesuan){
				// 		getApp().globalData.isaffirm=true
				// 		uni.switchTab({
				// 			url: '/pages/tabBar/transaction/index'
				// 		});
				// 	}else{
				// 		getApp().globalData.isaffirm=false
				// 		uni.navigateTo({
				// 			url:'/pages/tabBar/transaction/statement/index'
				// 		})
				// 	}
				// })
				// return

				isOnline(tell).then(res => {
					if (res.flag) {
						// let data = {
						// 	brokerId: broker_id,
						// 	authCode: auth_code,
						// 	userId: this.mobile,
						// 	appId: appid,
						// 	userProductInfo: product_info,
						// 	// serviceUrl: 'tcp:180.169.252.138:51207', //180.168.146.187:10130
						// 	serviceUrl: 'tcp://'+trader_url+':'+trader_port,  
						// 	// serviceUrl: 'tcp://'+trader2_url+':'+trader2_port, //24
						// 	password: this.code
						// }
						let data = {
							brokerId: this.dealData_1.broker_id,
							authCode: this.dealData_1.auth_code,
							userId: this.mobile,
							appId: this.dealData_1.appid,
							userProductInfo: this.dealData_1.product_info,
							serviceUrl: 'tcp://' + this.dealData_1.trader_url + ':' + this.dealData_1
								.trader_port,
							password: this.code
						}
						dcRichAlert.init(data, function(res) {
							if (res.data == 1) {
								uni.hideLoading();
								uni.showToast({
									icon: 'none',
									title: '登录成功！'
								})
								try {
									let userdata = {
										user: data.userId,
										password: data.password
									}
									
									uni.setStorageSync('userPosition', userdata);
									const ttime = uni.getStorageSync('globalTime')
									if (ttime) {
										let date = new Date()
										let year = date.getFullYear()
										let month = date.getMonth() + 1
										if (month < 10) month = '0' + month
										let day = date.getDate()
										let getDate = year.toString() + month.toString() + day.toString()
										if (getDate > ttime) {
											if (userdata) {
												userid = userdata.user + 'userid'
												markeid = userdata.user + 'markeid'
												bloAccountId = userdata.user + 'bloAccountid'
												condAccount = userdata.user + 'condAccount'
												overcondAccount = userdata.user + 'overcondAccount'
												uni.removeStorageSync(userid)
												uni.removeStorageSync(markeid)
												uni.removeStorageSync(bloAccountId)
												uni.removeStorageSync(condAccount)
												uni.removeStorageSync(overcondAccount)
											}
											uni.removeStorageSync('globalAccount')
											uni.removeStorageSync('markeAccount')
										}
									}
									frequencyInfo({
										account: that.mobile
									}).then(res => {
										//res
									})
								} catch (e) {
									//error
								}

								affirmJieSuan({
									account: that.mobile
								}).then(res => {
									if (res.jiesuan) {
										getApp().globalData.isAccount = true
										getApp().globalData.isaffirm = true
										setTimeout(() => {
											uni.switchTab({
												url: '/pages/tabBar/transaction/index'
											});
										}, 500)
									} else {
										getApp().globalData.isaffirm = false
										uni.navigateTo({
											url: '/pages/tabBar/transaction/statement/index'
										})
									}
								})
							} else {
								if(res.code==3){
									uni.hideLoading();
									uni.showToast({
										icon: 'none',
										title: '账号或密码错误！'
									})
								} else if (res.code == 140) {
									let info = {
										brokerId: this.dealData_1.broker_id,
										userId: this.mobile,
										password: this.code
									}
									uni.setStorageSync('forgetInfo', info)
									uni.hideLoading();
									uni.showModal({
										title: '温馨提示',
										content: '首次登录必须修改密码，请修改密码后重新登录',
										confirmText: '修改',
										success(res) {
											if (res.confirm) {
												uni.navigateTo({
													url: '../forgetPassword/index'
												})
											}
										}
									})
								} else {
									if (that.timeQuantum()) {
										that.ismobile=true
									} else {
										uni.showToast({
											icon: 'none',
											title: res.message
										})
									}
								}
							}
						})
					} else {
						uni.hideLoading();
						if (that.timeQuantum()) {
							that.ismobile=true
						} else {
							uni.showToast({
								icon: 'none',
								title: '登录失败'
							})
						}
					}
				}).catch(err => {
					uni.hideLoading();
					if (that.timeQuantum()) {
						that.ismobile=true
					} else {
						uni.showToast({
							icon: 'none',
							title: '登录失败'
						})
					}
				})
			},
			timeQuantum() {
				const date = parseTime(new Date().getTime()/1000, 'YYYY/MM/DD')
				let timestap = new Date().getTime()
				let morning = new Date(date+' 02:30:00').getTime()
				let forenoon = new Date(date+' 08:00:00').getTime()
				let afternoon = new Date(date+' 16:30:00').getTime()
				let evening = new Date(date+' 20:30:00').getTime()
				let status = false
				// 02:30 - 08:00
				if (timestap > evening && forenoon> timestap) {
					status = true
				}
				// 16:30 - 20:30
				if (timestap > afternoon && evening > timestap) {
					status = true
				}
				return status
			},
			noDeal() {
				uni.navigateTo({
					url: './nullLogin/nullLogin'
				})
				this.ismobile = false
			},
			initfa() {
			
			},
			Account() {
				try {
					dcRichAlert.reqQrySecAgentTradingAccount({}, function(res) {
						uni.showToast({
							icon: 'none',
							title: res.data
						})
					})
				} catch (e) {
					//error
				}
				// try {
				// 	reqQrySecAgentTradingAccount().then(res => {
				// 		uni.showToast({
				// 			icon: 'none',
				// 			title: res.data
				// 		})
				// 	})
				// } catch (e) {
				// 	// error
				// }

			},
		},
		onReady() {
			// this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #FFFFFF;
	}

	.mobile_cont {
		padding: 20px 0px 0px;
		text-align: center;

		.title {
			font-size: 18px;
			font-weight: bold;
		}

		.text {
			margin: 10px 0;
		}

		.time {
			color: #f33143;
		}

		.time,
		.buttom {
			font-size: 12px;
			display: flex;
			justify-content: space-around;
			align-items: center;
		}

		.buttom {
			// padding: 10px 0;
			margin-top: 15px;

			.left {
				border-top: 1rpx solid #E6E6E6;
				height: 40px;
				line-height: 40px;
				flex: 1;
			}

			.right {
				flex: 1;
				height: 40px;
				line-height: 40px;
				color: #EFAB00;
				border-top: 1rpx solid #E6E6E6;
				border-left: 1rpx solid #E6E6E6;
			}
		}
	}

	.nopass {
		padding: 10px;
		text-align: right;
		color: #EFAB00;
		font-size: 12px;
	}

	.custom-style {
		background: #EFAB00;
		color: #fff;
		height: 44px;
		width: 90%;
		line-height: 44px;
	}

	.deal {
		padding: 20px;
		text-align: center;
		font-size: 14px;
		color: #EFAB00;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.bottom_text {
		padding: 20px 0;
		text-align: center;
		font-size: 14px;
		color: #BDBDBD;
	}
</style>
